<template>
  <div class="app-container">
    <el-form
      class="box-container"
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="消息内容" prop="content">
        <el-input
          v-model="queryParams.content"
          placeholder="请输入消息内容"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="阅读状态" prop="readStatus">
        <el-select v-model="queryParams.readStatus" @change="handleQuery">
          <el-option value="1">未读</el-option>
          <el-option value="2">已读</el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <div class="box-container">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" size="mini" @click="updateAllRead"
            >全部设为已读</el-button
          >
        </el-col>
      </el-row>
    </div>

    <div class="box-container">
      <el-table
        v-loading="loading"
        :data="dataList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          label="消息类型"
          align="center"
          prop="type"
          width="120"
        >
          <template slot-scope="scope">
            {{ ["", "系统消息", "通知书"][scope.row.type] }}
          </template>
        </el-table-column>
        <el-table-column label="消息内容" align="left" prop="content" />
        <el-table-column
          label="发送时间"
          align="center"
          prop="createTime"
          width="150"
        >
          <template slot-scope="scope">
            {{ parseTime(scope.row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column
          label="阅读状态"
          align="center"
          prop="readStatus"
          width="120"
        >
          <template slot-scope="scope">
            <el-tag
              effect="dark"
              type="warning"
              size="small"
              v-if="scope.row.readStatus == 1"
              >未读</el-tag
            >
            <el-tag
              effect="dark"
              type="success"
              size="small"
              v-if="scope.row.readStatus == 2"
              >已读</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
          fixed="right"
          width="100"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-document"
              @click="detailOpen(scope.row)"
              >查看详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <el-dialog
      title="消息详情"
      :visible.sync="detailShow"
      width="950px"
      append-to-body
      :close-on-click-modal="false"
    >
      <div v-if="message.type == 1">{{ message.content }}</div>
      <div v-else class="enter-container">
        <div ref="enterDom" style="background-color: white; padding: 50px">
          <div class="title">进修录取通知书</div>
          <div>
            <span class="underline">{{ form.unitHospitalName || "" }}</span>
          </div>
          <div class="indent">
            经审核，同意<span class="underline"> {{ form.userName }} </span
            >，<span class="underline">
              进修工号：{{ form.registerNoticeVo.jobNumber || "（进修工号）" }}
            </span>
            同志来复旦大学附属华山医院进修
            <span class="underline"> {{ form.registerDeptName }} </span> 专业。
          </div>
          <div class="indent">
            进修期限：<span class="underline">
              {{ form.registerNoticeVo.startDate }}
            </span>
            至
            <span class="underline"> {{ form.registerNoticeVo.endDate }} </span
            >。<!--，共 <span class="underline"> {{ form.durationName }} </span>-->
          </div>
          <div class="indent">
            请务必在
            <span class="underline">
              <!-- {{ form.registerNoticeVo.startDate }} -->
              {{ form.registerNoticeVo.registerDate }}
            </span>
            凭本通知来我院报到。
          </div>
          <div class="indent">
            报到地点：<span class="underline">{{
              form.registerNoticeVo.registerAddress
            }}</span>
            华山医院地址: 乌鲁木齐中路12号
          </div>
          <div class="indent">
            报到时间：<span class="underline"
              >{{ form.registerNoticeVo.registerTime }}
            </span>
            邮编：200040
          </div>
          <div class="indent">
            交纳进修费{{ form.registerNoticeVo.engageMoney }}元，实验材料费{{
              form.registerNoticeVo.materialMoney
            }}元。{{
              form.joinMicrosutureTrain == 1
                ? "显微血管缝合操作培训费2800元"
                : ""
            }}
          </div>
          <div class="indent bolder">线上缴费操作流程</div>
          <div class="indent">
            第一步：微信关注公众号“华山医院财务资产管理部”，点击“云缴费”，并注册（微信快捷登录或手机号注册）；
          </div>
          <div class="indent">第二步：选择【进修费】，点击进入；</div>
          <div class="indent">
            第三步：输入身份证号，点击“查询”，出现缴费信息，点击“去支付”即可；
          </div>
          <div class="indent">
            第四步：缴费完成后，可在【凭证】或【我的-“缴费记录”】中查到已缴费的信息和电子发票，自行打印即可用于报销。
          </div>
          <div class="indent">
            关于线上缴费，如有疑问，请联系财务资产管理部：谢老师，联系电话：021-52888079。
          </div>
          <div class="indent">
            如持公务卡学员，报到之日至总院门诊10楼财务资产管理部谢老师处现场缴费、开票。未完成缴费者（公务卡除外）将不予报到。
          </div>
          <div class="indent">
            进修管理联系人：景老师 联系电话：021-52888960
          </div>
          <!-- <div class="indent">
            持公务卡学员，报到时请至财务科现场缴费、开票;
          </div>
          <div class="indent">
            其余学员请于报到之日前提前至少一周完成转账缴费，并通过问卷https://www.wjx.cn/vm/mR5S05G.aspx#
            上报缴费信息和开票信息。未完成缴费者(公务卡除外)不予报到。
          </div>
          <div class="indent bolder">转账信息如下:</div>
          <div class="indent bolder">
            户名: {{ form.registerNoticeVo.financialAccount }}
          </div>
          <div class="indent bolder">
            开户行: {{ form.registerNoticeVo.bankName }}
          </div>
          <div class="indent bolder">
            账号: {{ form.registerNoticeVo.bankCardNumber }}
          </div>
          <div class="indent bolder">转账时备注: 姓名+进修工号</div>
          <div class="indent bolder">
            联系电话: {{ form.registerNoticeVo.contactMan }} 联系人:
            {{ form.registerNoticeVo.contactPhone }}
          </div> -->
          <div class="indent bolder">{{ form.registerNoticeVo.remark }}</div>
          <div class="indent">此致</div>
          <div>敬礼</div>
          <div class="sign">
            <div>复旦大学附属华山医院教育处</div>
            <div>
              {{
                parseTime(
                  form.registerNoticeVo.createTime,
                  "{yyyy}年{mm}月{dd}日"
                )
              }}
            </div>
            <img
              class="img"
              src="../../../../assets/images/official-seal.png"
            />
          </div>
          <div style="border-bottom: 1px dashed #898a8c"></div>
          <div v-html="form.registerNoticeVo.noticeContent"></div>
        </div>
        <div style="color: #a52529; text-align: center">
          提示：进修院区具体安排由科室进行，如需了解可通过总机52889999转至各具体科室询问。
        </div>

        <div class="tips">
          请在阅读完录取通知书后<span class="underline"
            >确认是否可以按时来我院报到？</span
          >
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="detailShow = false">取 消</el-button>
        <el-button
          type="primary"
          @click="downloadWord"
          v-if="form.noticeStatus == 3"
          >下载通知书</el-button
        >
        <el-button
          v-if="form.noticeStatus == 2"
          type="primary"
          @click="updateStatus(5)"
          plain
          >不能如期前来</el-button
        >
        <el-button
          v-if="form.noticeStatus == 2"
          type="primary"
          @click="updateStatus(3)"
          >确认按时报到</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getCurrNoReadCount,
  listMessageInfo,
  updateMessageInfo,
} from "@/api/business/messageInfo";
import {
  getRegisterInfo,
  updateRegisterInfo,
} from "@/api/business/registerInfo";
import { updateAllReadRegisterPerson } from "@/api/business/registerPerson";
import { parseTime } from "@/utils/ruoyi";
import domtoimage from "dom-to-image";

export default {
  name: "message",
  data() {
    return {
      // 根路径
      baseURL: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 消息通知表格数据
      dataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        content: null,
        readStatus: null,
      },
      // 查看详情
      detailShow: false,
      message: {},
      form: {
        registerNoticeVo: {},
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    parseTime,
    /** 查询消息通知列表 */
    getList() {
      this.loading = true;
      listMessageInfo(this.queryParams).then((response) => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 全部设为已读
    updateAllRead() {
      this.$modal
        .confirm("是否确认全部设置为已读？")
        .then(function () {
          return updateAllReadRegisterPerson();
        })
        .then(() => {
          this.updateNoRead();
          this.getList();
          this.$modal.msgSuccess("设置成功");
        })
        .catch(() => {});
    },
    // 查看详情
    detailOpen(row) {
      console.log(row);
      this.message = row;
      try {
        updateMessageInfo({ id: row.id, readStatus: 2 }).then((response) => {
          this.updateNoRead();
          this.getList();
          if (row.type == 2) {
            getRegisterInfo(row.refId).then((response) => {
              console.log("内容", response);
              if (response.code == 500) {
                this.$modal.msgError(response.msg);
                return;
              } else if (response.code == 200 && response.data != null) {
                this.form = response.data;
                this.detailShow = true;
              } else {
                this.$modal.msgError("系统错误");
              }
            });
          } else {
            this.detailShow = true;
          }
        });
      } catch (error) {
        console.log(error);
      }
    },
    updateNoRead() {
      getCurrNoReadCount().then((response) => {
        if (response.data == 0) {
          this.$store.commit("SET_NO_READ_COUNT", null);
        } else {
          this.$store.commit("SET_NO_READ_COUNT", response.data);
        }
      });
    },
    // 3确认按时报到、5不能如期前来
    updateStatus(value) {
      let message = "";
      let data = {
        id: this.form.id,
        phoneNumber: this.form.phoneNumber,
        noticeStatus: value,
        // 待报到
        sureStatus: null,
        // 确认按时报道标记
        sureFlag: null,
      };
      if (value == 3) {
        message = "您确定按时报到吗？";
        data.sureStatus = 1;
        data.sureFlag = 1;
      } else if (value == 5) {
        message = "您确定不能如期前来吗？";
      }
      this.$modal.confirm(message).then(() => {
        updateRegisterInfo(data).then(() => {
          this.$modal.msgSuccess("保存成功");
          this.detailShow = false;

          if (value == 3) {
            getRegisterInfo(this.form.id).then((response) => {
              this.form = response.data;
            });
          } else {
            this.getList();
          }
        });
      });
    },
    // 下载通知书
    downloadWord() {
      domtoimage.toPng(this.$refs.enterDom).then((dataUrl) => {
        console.log(dataUrl);
        let a = document.createElement("a");
        let event = new MouseEvent("click");
        a.download = "进修录取通知书.jpg";
        a.href = dataUrl;
        a.dispatchEvent(event);
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
  },
};
</script>
<style lang="scss" scoped>
.enter-container {
  font-family: "Microsoft YaHei";
  font-size: 18px;
  padding: 0 15px;
  line-height: 30px;

  ::v-deep .ql-align-center {
    text-align: center;
  }

  .btn-box {
    display: flex;
    justify-content: flex-end;
  }

  .title {
    text-align: center;
    font-size: 26px;
    font-weight: bolder;
    letter-spacing: 0.5em;
  }

  .indent {
    text-indent: 2em;
  }

  .bolder {
    font-weight: bolder;
  }

  .underline {
    text-decoration: underline;
  }

  .title2 {
    margin: 15px 0;
    text-align: center;
    font-size: 26px;
    font-weight: bolder;
  }

  .sign {
    margin-top: 40px;
    text-align: right;
    font-weight: bolder;
    position: relative;

    .img {
      position: absolute;
      top: -60px;
      right: 20px;
      width: 130px;
      height: 130px;
    }
  }

  .tips {
    margin-top: 50px;
    text-align: center;
    font-weight: bolder;
    color: #a52529;
  }
}
</style>
